package sctu.edu.java.year2016.team01.xuhanmei.day20160606.LearnThread;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Administrator on 2016/6/6.
 */
public class JionTest extends JFrame {
    private Thread threadA;//创建线程
    private Thread threadB;
    final JProgressBar progressBar = new JProgressBar();//创建进度条
    final JProgressBar progressBar2 = new JProgressBar();

    int count = 0;

    public static void main(String[] args) {
        init(new JionTest(), 100, 100);//在主方法中调用init方法，并实例化JionTest，给width和height赋值
    }


    public JionTest() {//写这个类的构造方法
        super();
        getContentPane().add(progressBar, BorderLayout.NORTH);//设置进度条的位置
        getContentPane().add(progressBar2, BorderLayout.SOUTH);
        progressBar.setStringPainted(true);
        progressBar2.setStringPainted(true);//设置进度条显示数字字符


        threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    progressBar.setValue(++count);
                    try {
                        threadA.sleep(100);//休眠0.1秒
                        threadB.join();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

            }
        });

        threadA.start();//启动


        threadB = new Thread(new Runnable() {
            int count = 0;

            @Override
            public void run() {
                while (true) {
                    progressBar2.setValue(++count);
                    try {
                        threadB.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (count == 100) {
                        break;
                    }
                }

            }
        });
        threadB.start();
    }


    private static void init(JFrame frame, int width, int height) {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setVisible(true);
    }
}
